﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DesertKharonIrcPlugin.Emotes.Twitch
{
    public partial class TwitchEmoteCacheUpdateDialog : Form
    {
        private string _valueMaxValueDisplayFormat = "{0} / {1}";

        private TwitchEmoteCacheUpdater _importer;
        private string _destinationFolder;

        public TwitchEmoteCacheUpdateDialog(string destinationFolder)
        {
            _destinationFolder = destinationFolder;
            InitializeComponent();
        }

        private void TwitchEmoteCacheUpdateDialog_Load(object sender, EventArgs e)
        {
            _importer = new TwitchEmoteCacheUpdater(_destinationFolder, false, false, true);
            _importer.ProgressChanged += Importer_ProgressChanged;
            _importer.Completed += Importer_Completed;
            _importer.Start();
        }

        private void TwitchEmoteCacheUpdateDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_complete == false)
            {
                _importer.Cancel();
                e.Cancel = true;
            }
        }

        private void TwitchEmoteCacheUpdateDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            _importer.Dispose();
        }

        private void Importer_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            TwitchEmoteCacheUpdaterState state = e.UserState as TwitchEmoteCacheUpdaterState;
            if (state != null)
            {
                gatheringInformationProgressBar.Maximum = state.InformationMaximum > 0 ? state.InformationMaximum : 1;
                gatheringInformationProgressBar.Value = state.InformationCount > gatheringInformationProgressBar.Maximum ? gatheringInformationProgressBar.Maximum : state.InformationCount;
                gatheringInformationProgressInfoLabel.Text = String.Format(_valueMaxValueDisplayFormat, state.InformationCount, state.InformationMaximum);

                downloadImagesProgressBar.Maximum = state.DownloadMaximum > 0 ? state.DownloadMaximum : 1;
                downloadImagesProgressBar.Value = state.DownloadCount > downloadImagesProgressBar.Maximum ? downloadImagesProgressBar.Maximum : state.DownloadCount;
                downloadImagesProgressInfoLabel.Text = String.Format(_valueMaxValueDisplayFormat, state.DownloadCount, state.DownloadMaximum);
            }
        }

        private bool _complete = true;
        private void Importer_Completed(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
                MessageBox.Show(this, "Cache updated.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                if (e.Cancelled)
                    MessageBox.Show(this, "Cache update cancelled.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (e.Error != null)
                    MessageBox.Show(this, String.Format("Cache update error: {0}", e.Error.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            _complete = true;
            Close();
        }
    }
}
